



global filename jmp_results_naics


use "stata data/regsave/FE_new_all_customer_1", clear

append using "stata data/regsave/FE_new_all_customer_0"

foreach customer of numlist 0/1 {
append using "stata data/regsave/FE_new_all_coastal_customer_`customer'"
append using "stata data/regsave/FE_new_all_inland_customer_`customer'"
}




keep if var == "pdp"
duplicates drop
order  cz


capture order type column 
gen temp_term = regexm(var,"temp")
drop if temp_term == 1
drop temp_term

//here I drop the pdp one in the marginal since the marginal effect comes from the pdp_non_event variable


replace var = "pdp" if var == "pdp_event_day"


drop tstat	ci_lower	ci_upper	N	r2

gen stars = ""
replace stars = "*" if pval<.1
replace stars = "**" if pval<.05
replace stars = "***" if pval<.01

gen label = ""
replace label = "Peak pricing" if var == "pdp"


capture file close table1		
file open table1 using "latex/$filename.tex", write text replace

file write table1 "\label{$filename}" _n
file write table1 "\vspace{-2mm}" _n
*file write table1 "\footnotesize" _n
*file write table1 "\begin{tabular}{lcccccc}" _n //this is where you change justification. Options are l c r

file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lcc@{\hskip .5cm}cc@{\hskip .5cm}cc}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

*file write table1 "\multicolumn{1}{c}{$\begin{matrix}\text{Baseline}\\ \text{Territory} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n
*file write table1 " Energy use classification && Median & & &Thirds	\\" _n 
*file write table1 "  \cline{2-4} \cline{5-7} \\" _n 
*file write table1 "[0.2em]" _n

file write table1 "  & \multicolumn{2}{c}{ All PG\&E} & \multicolumn{2}{c}{Coastal} & \multicolumn{2}{c}{Inland} \\" _n 


*file write table1 "  && Structural winners & & &Structural losers\\" _n 
file write table1 "  \cline{2-3} \cline{4-5} \cline{6-7} \\" _n 
file write table1 "[-0.8em]" _n
file write table1 "  & (1) & (2) & (3) & (4) & (5) & (6)  \\" _n
file write table1 "[0.2em]" _n

local customer_facing "\multicolumn{1}{c}{$\begin{matrix}\text{Customer}\\ \text{facing} \end{matrix}$}"
local no_customer_facing "\multicolumn{1}{c}{$\begin{matrix}\text{Non-cust}\\ \text{facing} \end{matrix}$}"

file write table1 "& `customer_facing' & `no_customer_facing' & `customer_facing' & `no_customer_facing' & `customer_facing' & `no_customer_facing'	\\" _n
*file write table1 "&\multicolumn{1}{c}{$\begin{matrix}\text{Customer}\\ \text{facing} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n


*file write table1 "  & Customer facing & Not customer facing & FS FE & RD & FS FE& FS RD  \\" _n
file write table1 "\hline \\" _n
file write table1 "[-0.7em]" _n



capture gen row = _n

gen avg_kwh_high_EI_label = ""

tostring customer_facing, replace

gen column_id = cz + "_customer_facing_" + customer_facing
order column_id

order column_id


local col1 "all_customer_facing_1"
local col2 "all_customer_facing_0"
local col3 "coastal_customer_facing_1"
local col4 "coastal_customer_facing_0"
local col5 "inland_customer_facing_1"
local col6 "inland_customer_facing_0"


foreach v in "pdp"  { //this first loop looks through pdp_high_EIs producing and services providing

foreach col in "`col1'" "`col2'" "`col3'" "`col4'" "`col5'" "`col6'" { //"`col2'" "`col3'" "`col4'" "`col5'" "`col6'" { //this loop loops through event day and non-event day

local panel "event"  // "non_event" "marginal"{ //this loop loops through event day and non-event day

sum row if var == "`v'" & col == "`col'" & type == "`panel'" //this grabs the correct row. So goods event is row number ___ - I then grab that NUMBER observation out of variable
local r = r(mean) //just a way to indicate what row you're on from the sum term above


*local name_`col'_`panel' = label[`r'] 
local c1_`col' = "\$"+string(coef[`r'],"%6.4f")+ "$" + stars[`r'] 
local c2_`col' = "(" +"\$"+ string(stderr[`r'],"%6.4f")+"$"  + ")"

local avg_kwh_high_EI_label = avg_kwh_high_EI_label[`r'] 
*local s1_`col'_`panel' = string(avg_kwh_high_EI[`r'],"%-9.2f")
}

file write table1 " Peak pricing & `c1_`col1'' & `c1_`col2'' & `c1_`col3''  & `c1_`col4'' & `c1_`col5'' & `c1_`col6''    \\" _n //the COEF line. Include title
file write table1 " 					 & `c2_`col1'' & `c2_`col2'' & `c2_`col3''  & `c2_`col4''  & `c2_`col5'' & `c2_`col6''   \\" _n //the standard error line. The & at the front is b/c no titles on this line
file write table1 "[1.5ex] " _n //this adds vertical space between the se/coef groupings

}

///
///Marginal
///


gen obs_label = "Observations"
gen firms_label = "Establishments"
gen event_day_kwh_label = "Event day kWh usage"

capture drop money_pdp_saved
gen money_pdp_saved = coef * 4 * .6 * -1
gen money_pdp_saved_label = "Dollar savings"

gen temp_f_label = "Average temperature"


foreach v in "pdp"  { //this first loop looks through pdp_high_EIs producing and services providing

foreach col in "`col1'" "`col2'" "`col3'" "`col4'" "`col5'" "`col6'" { //  "`col3'" "`col4'" "`col5'" "`col6'" { //this loop loops through event day and non-event day

sum row if var == "`v'" & col == "`col'" & type == "event" //this grabs the correct row. So goods event is row number ___ - I then grab that NUMBER observation out of variable
local r = r(mean) //just a way to indicate what row you're on from the sum term above


local obs_label = obs_label[`r'] 
local s1_`col' = "\$" + string(obs[`r'],"%-9.0fc") +"$"


local firms_label = firms_label[`r'] 
local s2_`col' = "\$" + string(firms[`r'],"%-9.0fc") +"$"

local event_day_kwh_label = event_day_kwh_label[`r'] 
local s3_`col' = "\$" + string(avg_kwh[`r'],"%-9.2fc") +"$"


local temp_f_label = temp_f_label[`r'] 
local s4_`col' = "\$" + string(temp_f[`r'],"%-9.0fc") +"$"


}
*file write table1 "`firms_label' & `s1_5' & `s1_8'  \\" _n 
file write table1 "`obs_label' 		 & `s1_`col1'' & `s1_`col2'' & `s1_`col3'' & `s1_`col4'' & `s1_`col5'' & `s1_`col6''  \\" _n 
file write table1 "`firms_label' 		 & `s2_`col1'' & `s2_`col2'' & `s2_`col3'' & `s2_`col4'' & `s2_`col5'' & `s2_`col6''  \\" _n 
file write table1 "`event_day_kwh_label' & `s3_`col1'' & `s3_`col2'' & `s3_`col3'' & `s3_`col4'' & `s3_`col5'' & `s3_`col6'' \\" _n 
file write table1 "`temp_f_label' 		 & `s4_`col1'' & `s4_`col2'' & `s4_`col3'' & `s4_`col4'' & `s4_`col5'' & `s4_`col6'' \\" _n 


}
*




file write table1 "\hline" _n
file write table1 "\end{tabular}" _n

file close table1		


